//
//  JQFXHWebViewController.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/12.
//

import Foundation
import UIKit
import WebKit

typealias WebViewBlock = (Int)->Void


class JQFXHWebViewController: UIViewController {
    private var jqfxhShoweeAgree: Bool
    private var jqfxhUrlccString: String
    private var jqfxhWebddView: WKWebView!
    
    var block : WebViewBlock?

    

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func jqfxhCloseWeb() {
        self.dismiss(animated: true)
    }
    
    init(jqfxhUrlccString: String, jqfxhTitle: String? = nil, jqfxhShoweeAgreeBtn: Bool = false) {
        self.jqfxhUrlccString = jqfxhUrlccString
        self.jqfxhShoweeAgree = jqfxhShoweeAgreeBtn
        super.init(nibName: nil, bundle: nil)
        self.title = title
    }
    
    @objc func jqfxhAgreeffBtnClick() {
        block?(0)
        self.dismiss(animated: true)

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let config = WKWebViewConfiguration()
        let bottonHeight = self.jqfxhShoweeAgree ? 50 : 0.01
        jqfxhWebddView = WKWebView(frame: .zero, configuration: config)
        self.view.addSubview(jqfxhWebddView)
        jqfxhWebddView.snp.makeConstraints { make in
            make.top.equalTo(self.view).offset(50)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom-bottonHeight)
        }
        
        if self.jqfxhShoweeAgree {
            self.view.addSubview(jqfxhAgreeffBtn)
            jqfxhAgreeffBtn.snp.makeConstraints { make in
                make.left.equalTo(self.view).offset(20)
                make.right.equalTo(self.view).offset(-20)
                make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom)
                make.height.equalTo(42)
            }
        }
        
        let jqfxhTitleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: jqfxhScreenWidth, height: 50))
        jqfxhTitleLabel.backgroundColor = UIColor.white
        jqfxhTitleLabel.text = self.title
        jqfxhTitleLabel.textAlignment = .center
        jqfxhTitleLabel.font = UIFont.jdqhBoldTitle16
        jqfxhTitleLabel.textColor = UIColor.black
        self.view.addSubview(jqfxhTitleLabel)


        let jqfxhCloseBtn = UIButton(type: .custom)
        jqfxhCloseBtn.setTitle("关闭", for: .normal)
        jqfxhCloseBtn.setTitleColor(UIColor.black, for: .normal)
        jqfxhCloseBtn.frame = CGRect(x: 20, y: 0, width: 40, height: 50)
        jqfxhCloseBtn.addTarget(self, action: #selector(jqfxhCloseWeb), for: .touchUpInside)
        view.addSubview(jqfxhCloseBtn)
        
        jqfxhLoadWebggPage()
    }
    

    

    private func jqfxhLoadWebggPage() {
        guard let url = URL(string: jqfxhUrlccString) else {
            jqfxhShowwwError("无效的链接")
            return
        }
        let request = URLRequest(url: url)
        jqfxhWebddView.load(request)
    }


    private func jqfxhShowwwError(_ message: String) {
        let alert = UIAlertController(title: "错误", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "关闭", style: .default))
        present(alert, animated: true)
    }
    
    lazy var jqfxhAgreeffBtn : UIButton = {
        let jqfxhAgreeffBtn = UIButton(type: .custom)
        jqfxhAgreeffBtn.setTitle("同意并继续", for: .normal)
        jqfxhAgreeffBtn.setTitleColor(UIColor.white, for: .normal)
        jqfxhAgreeffBtn.backgroundColor = UIColor.jqfxhThemexxRed
        jqfxhAgreeffBtn.titleLabel?.font = UIFont.jqfxhRegular16
        jqfxhAgreeffBtn.addTarget(self, action: #selector(jqfxhAgreeffBtnClick), for: .touchUpInside)
        jqfxhAgreeffBtn.layer.cornerRadius = 4
        jqfxhAgreeffBtn.layer.masksToBounds = true
        return jqfxhAgreeffBtn
    }()
}


extension JQFXHWebViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.contentOffset.x = 0
    }
}
